<template>
  <div>
    <el-dialog title="新增工单" :visible="showAddDialog" @close="close">
      <el-form ref="addTask" label-width="120px" :model="formData" :rules="rules">
        <el-form-item prop="innerCode" label="设备编号">
          <el-input
            v-model="formData.innerCode"
            placeholder="请输入"
            style="width: 80%"
            @input="inputfn"
          />
        </el-form-item>

        <el-form-item label="工单类型" prop="statuStyle">
          <el-select
            v-model="formData.statuStyle"
            placeholder="请选择"
            style="width: 80%"
          >
            <!-- 遍历列表，生成选项 -->
            <el-option
              label="补货工单"
              value="2"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="补货数量" prop="ChannelList">
          <template>
            <el-button type="text" @click="showvmdialog=true"><i class="el-icon-document" style="margin:0 5px" />补货清单</el-button>
          </template>
        </el-form-item>

        <el-form-item label="运营人员" prop="operaterole">
          <el-select
            v-model="formData.operaterole"
            placeholder="请选择"
            style="width: 80%"
          >
            <!-- 遍历列表，生成选项 -->
            <el-option
              v-for="item in OperatorList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="desc" label="备注">
          <el-input
            v-model="formData.desc"
            placeholder="请输入备注（不超过40字）"
            style="width: 80%"
          />
        </el-form-item>

        <el-form-item>
          <!-- 按钮 -->
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button
                style="background-color:#fbf4f0;"
                @click="quitfn"
              >取消</el-button>
              <el-button
                type="primary"
                style="background-color:#ff6c29;"
                @click="btnOK"
              >确认</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>

    </el-dialog>
    <!-- 补货清单 -->
    <el-dialog title="补货详情" :visible="showvmdialog" @close="close1">
      <div class="dialog-content">
        <el-table :data="ChannelList" height="250">
          <!-- 放置列 -->
          <el-table-column prop="channelCode" align="center" label="货道编号" />
          <el-table-column prop="sku.skuName" align="center" label="商品名称" />
          <el-table-column prop="currentCapacity" align="center" label="当前数量" />
          <el-table-column prop="maxCapacity" align="center" label="还可添加" />
          <el-table-column align="center" label="补满数量">
            <!-- 自定义列结构 -->
            <template v-slot="{row}">
              <el-input-number
                size="mini"
                :value="row.maxCapacity-row.currentCapacity"
                controls-position="right"
                style="width: 80%;display: flex; flex-direction: column"
              />
            </template>
          </el-table-column>

        </el-table>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col>
            <el-button
              style="background-color:#fbf4f0;margin-left:40%"
              @click="showvmdialog = false"
            >取消</el-button>
            <el-button
              type="primary"
              style="background-color:#ff6c29;"
              @click="showvmdialog = false"
            >确认</el-button>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getOperatorList } from '@/api/businessTask'
export default {
  name: 'AddTask',
  props: {
    showAddDialog: {
      type: Boolean,
      require: false
    }
  },
  data() {
    return {
      showvmdialog: false,
      OperatorList: [],
      ChannelList: [],
      formData: {
        innerCode: '',
        statuStyle: '',
        operaterole: '',
        desc: ''
      },
      rules: {
        innerCode: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        statuStyle: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        operaterole: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 40, message: '最多输入40字', trigger: 'change' }
        ],
        ChannelList: [
          {
            validator: (rule, value, callback) => {
              if (this.ChannelList.length) {
                callback()
              } else {
                callback(new Error('暂无数据'))
              }
            },
            trigger: 'submit'
          }
        ],
        // 部门编码
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '部门编码的长度为2-10个字符',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              // 判断是否是编辑模式
              if (this.formData.id) {
                // 编辑场景
                const result = this.origintree.filter(item => item.id !== this.formData.id)
                // result数组中是否存在 value值
                if (result.some(item => item.code === value)) {
                  callback(new Error('部门中已经有该编码了'))
                } else {
                  callback()
                }
              } else {
                if (this.origintree.some((item) => item.code === value)) {
                  callback(new Error('部门中已经有该编码了'))
                } else {
                  callback()
                }
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  watch: {
    'formData.innerCode': {
      handler(val) {
        this.getOpAndChalList(val)
      },
      deep: true
    }
  },
  created() {
  },
  methods: {
    async getOpAndChalList() {
      const res = await getOperatorList(this.formData.innerCode)
      console.log(res)
      if (res && res.length > 0) {
        this.OperatorList = res
      } else {
        this.OperatorList = []
      }
      // const res1 = await getChannelList(this.formData.innerCode)
      // if (res1 && res1.length > 0) {
      //   this.ChannelList = res1
      // } else { this.ChannelList = [] }
    },
    async inputfn() {
      await this.getOpAndChalList()
    },
    close() {
      // this.OperatorList = [{ userId: '', userName: '无数据' }],
      // this.ChannelList = [],
      // this.formData = {
      //   innerCode: '',
      //   statuStyle: '',
      //   operaterole: '',
      //   desc: ''
      // },
      this.$refs.addTask.resetFields() // 重置表单
      this.$emit('update:showAddDialog', false)
    },
    close1() {
      this.showvmdialog = false
    },
    // 点击取消时调用
    quitfn() {
      this.close()
    },
    // 点击确定时调用
    btnOK() {
      // this.$refs.addTask.validate(async(right) => {
      //   if (right) {
      //     if (this.formData.id) {
      //       await updateDepartment(this.formData)
      //     } else {
      //       // 新增场景
      //       await addDepartment({ ...this.formData, pid: this.currentNodeId })
      //     }
      //     // 通知父组件更新
      //     this.$emit('updateTaskList')
      //     this.close()
      //   }
      // })
    },
    async getDepartmentDetail() {
      // this.formData = await getDepartmentDetail(this.currentNodeId)
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
    overflow: auto;
    max-height: calc(100vh - 180px); /* 根据您的需要调整数字 */
    padding: 20px;
  }
  .el-dialog__header,
  .el-dialog__footer {
    position: sticky;
    z-index: 1;
  }
  .el-dialog__header {
    top: 0;
  }
  .el-dialog__footer {
    bottom: 0;
  }
  ::v-deep .el-dialog{
  border-radius: 10px;
  }
</style>
